package com.atguigu.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.dto.h5.ProductSkuDto;
import com.atguigu.entity.product.Product;
import com.atguigu.entity.product.ProductDetails;
import com.atguigu.entity.product.ProductSku;
import com.atguigu.mapper.ProductDetailsMapper;
import com.atguigu.mapper.ProductMapper;
import com.atguigu.mapper.ProductSkuMapper;
import com.atguigu.service.ProductService;
import com.atguigu.vo.h5.ProductItemVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public List<ProductSku> selectProductSkuBySale() {
        return productSkuMapper.selectProductSkuBySale();
    }

    @Override
    public ProductItemVo detail(Long skuId) {
        //1 创建vo对象，用于封装最终数据
        ProductItemVo productItemVo = new ProductItemVo();

        //2 根据skuId获取sku信息
        ProductSku productSku = productSkuMapper.getById(skuId);

        //3 根据第二步获取sku，从sku获取productId，获取商品信息\
        Long productId = productSku.getProductId();
        Product product = productMapper.getById(productId);

        //4 productId，获取商品详情信息
        ProductDetails productDetails = productDetailsMapper.getByProductId(productId);

        //5 封装map集合 == 商品规格对应商品skuId信息\
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        //根据商品id获取商品所有sku列表
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productId);
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        //6 把需要数据封装到productItemVo里面
        productItemVo.setProduct(product);
        productItemVo.setProductSku(productSku);
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);

        //封装详情图片 list集合
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));

        //封装轮播图 list集合
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));

        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        return productItemVo;
    }

    @Override
    public PageInfo<ProductItemVo> page(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page, limit);
        List<ProductItemVo> byPage = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(byPage);
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.getById(skuId);
    }
}
